home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-10-31 | 6.1 KB | 233 lines |
- //******************************************************************************
- // Typewriter.java: Applet
- //
- // Created by Ricardo Basto
- // July 14, 1997
- //
- // You are free to use if no modification is done to the original file
- //******************************************************************************
-
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
-
- public class Typewriter extends Applet implements Runnable
- {
- private Thread m_Typewriter = null;
-
- // Members for applet parameters
- // <type> <MemberVar> = <Default Value>
- //--------------------------------------------------------------------------
- private String m_Text = "Ricardo Basto's Typewriter|Please e-mail me at|ricbasto@usa.net|";
- private String m_Target = "mailto:ricbasto@usa.net";
- private Color m_BGColor = Color.white;
- private Color m_FGColor = Color.black;
- private Color m_HLColor = Color.blue;
- private int m_FontSize = 20;
- private String m_FontName = "Dialog";
- private int m_Delay = 100;
-
- // Parameter names
- //--------------------------------------------------------------------------
- private final String PARAM_Text = "Text";
- private final String PARAM_Target = "Target";
- private final String PARAM_BGColor = "BGColor";
- private final String PARAM_FGColor = "FGColor";
- private final String PARAM_HLColor = "HLColor";
- private final String PARAM_FontSize = "FontSize";
- private final String PARAM_FontName = "FontName";
- private final String PARAM_Delay = "Delay";
-
- // Private data members
- //--------------------------------------------------------------------------
- private int StartY = 14;
- private int CurrentY;
- private int lPosition = -1;
- private int LastPos = 0;
-
-
- public String getAppletInfo()
- {
- return "Name: Typewriter\r\n" +
- "Author: Ricardo Basto\r\n" +
- "Created with Microsoft Visual J++ Version 1.1";
- }
-
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_Text, "String", "The text to be typed" },
- { PARAM_Target, "String", "Target URL" },
- { PARAM_BGColor, "int", "Background color" },
- { PARAM_FGColor, "int", "Foreground color" },
- { PARAM_HLColor, "int", "Highlight color" },
- { PARAM_FontSize, "int", "Font size in pixels" },
- { PARAM_FontName, "String", "Font's typeface name" },
- { PARAM_Delay, "int", "Typying delay" },
- };
- return info;
- }
-
- public void init()
- {
- String param;
-
- // Text: The text to be typed
- //----------------------------------------------------------------------
- param = getParameter(PARAM_Text);
- if (param != null)
- m_Text = param;
-
- // Text: Target URL
- //----------------------------------------------------------------------
- param = getParameter(PARAM_Target);
- if (param != null)
- m_Target = param;
-
- // BGColor: Background color
- //----------------------------------------------------------------------
- param = getParameter(PARAM_BGColor);
- if (param != null)
- m_BGColor = new Color(Integer.parseInt(param));
-
- // FGColor: Foreground color
- //----------------------------------------------------------------------
- param = getParameter(PARAM_FGColor);
- if (param != null)
- m_FGColor = new Color(Integer.parseInt(param));
-
- // HLColor: Highlight color
- //----------------------------------------------------------------------
- param = getParameter(PARAM_HLColor);
- if (param != null)
- m_HLColor = new Color(Integer.parseInt(param));
-
- // FontSize: Font size in pixels
- //----------------------------------------------------------------------
- param = getParameter(PARAM_FontSize);
- if (param != null)
- m_FontSize = Integer.parseInt(param);
-
- // FontName: Font's typeface name
- //----------------------------------------------------------------------
- param = getParameter(PARAM_Text);
- if (param != null)
- m_FontName = param;
-
- // Delay: Typying delay
- //----------------------------------------------------------------------
- param = getParameter(PARAM_Delay);
- if (param != null)
- m_Delay = Integer.parseInt(param);
-
-
- // Parameter Validation
- //----------------------------------------------------------------------
- if (m_Delay < 1) m_Delay = 1;
- if (m_FontSize < 2) m_FontSize = 2;
-
-
- // Initialization Code
- //----------------------------------------------------------------------
- StartY = m_FontSize + 2;
- CurrentY = StartY;
-
- setForeground(m_FGColor);
- setBackground(m_BGColor);
- setFont(new Font(m_FontName, Font.BOLD, m_FontSize));
- }
-
-
- // All drawing code is placed in the paint event
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- if (m_Text.charAt(lPosition) == '|')
- {
- CurrentY-=2;
- if (CurrentY > -3)
- lPosition--;
- else
- {
- CurrentY = StartY;
- LastPos = ++lPosition;
- }
- }
-
- g.drawString(m_Text.substring(LastPos, lPosition+1), 0, CurrentY);
- }
-
- public void start()
- {
- if (m_Typewriter == null)
- {
- m_Typewriter = new Thread(this);
- m_Typewriter.start();
- }
- }
-
- public void stop()
- {
- if (m_Typewriter != null)
- {
- m_Typewriter.stop();
- m_Typewriter = null;
- }
- }
-
- // All string manipulation code is placed in the thread
- //--------------------------------------------------------------------------
- public void run()
- {
- while (true)
- {
- try
- {
- lPosition++;
- if (lPosition > m_Text.length())
- {
- lPosition = -1;
- LastPos = 0;
- Thread.sleep(m_Delay * 10);
- }
- else
- {
- repaint();
-
- Thread.sleep(m_Delay);
- }
- }
- catch (InterruptedException e)
- {
- stop();
- }
- }
- }
-
- public boolean mouseDown(Event evt, int x, int y)
- {
- try
- {
- getAppletContext().showDocument(new URL(m_Target));
- }
- catch (MalformedURLException e)
- {
- m_Text = "Error|MalformedURLException|Target URL invalid|";
- }
- return true;
- }
-
- public boolean mouseEnter(Event evt, int x, int y)
- {
- setForeground(m_HLColor);
- return true;
- }
-
- public boolean mouseExit(Event evt, int x, int y)
- {
- setForeground(m_FGColor);
- return true;
- }
- }
-